﻿using System;
using System.Windows.Forms;
using LDSCore;
using LDSCore.Result;
using LDSModel.Dal;
using LDSProject.Properties;

namespace LDSProject.Forms
{
    public partial class ModifyPasswordForm : Form
    {
        public ModifyPasswordForm()
        {
            InitializeComponent();
        }

        private void ModifyPasswordForm_Load(object sender, EventArgs e)
        {
            lbUserName.Text = UserService.CurrentUser.UserName;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Check(tbOldPassword.Text.Trim(), tbNewPassword.Text.Trim(), tbReNewPassword.Text.Trim()))
            {
                Modify(lbUserName.Text.Trim(), tbOldPassword.Text.Trim(), tbNewPassword.Text.Trim());
            }
        }

        private bool Check(string oldPwd, string newPwd, string reNewPwd)
        {
            if (string.IsNullOrWhiteSpace(oldPwd))
            {
                tbOldPassword.Focus();
                tbOldPassword.SelectAll();
                lbInfo.Text = Resources.PasswordIsNull;
                lbInfo.Visible = true;
                return false;
            }
            if (tbOldPassword.Text.Trim().Length < 6)
            {
                tbOldPassword.Focus();
                tbOldPassword.SelectAll();
                lbInfo.Text = @"请输入6位以上密码！";
                lbInfo.Visible = true;
                return false;
            }
            if (string.IsNullOrWhiteSpace(newPwd))
            {
                tbNewPassword.Focus();
                tbNewPassword.SelectAll();
                lbInfo.Text = Resources.NewPasswordIsNull;
                lbInfo.Visible = true;
                return false;
            }
            if (string.IsNullOrWhiteSpace(reNewPwd))
            {
                tbReNewPassword.Focus();
                tbReNewPassword.SelectAll();
                lbInfo.Text = Resources.ConfirmPasswordIsNull;
                lbInfo.Visible = true;
                return false;
            }
            if (!newPwd.Equals(reNewPwd))
            {
                tbNewPassword.Focus();
                tbNewPassword.SelectAll();
                lbInfo.Text = Resources.ConfirmPasswordIsDifferent;
                lbInfo.Visible = true;
                return false;
            }
            return true;
        }

        private void Modify(string userName, string oldPwd, string newPwd)
        {
            OperationResult result = UserService.ModifyPwd(userName, Utils.EncryptMd5(oldPwd), Utils.EncryptMd5(newPwd));

            if (result.Success)
            {
                MessageBox.Show(this, Resources.ModifyPasswordIsSuccess);
                Close();
            }
            else
            {
                tbOldPassword.Focus();
                tbOldPassword.SelectAll();
                lbInfo.Text = result.Code.ToMessage();
                lbInfo.Visible = true;
            }
        }

        private void tbOldPassword_TextChanged(object sender, EventArgs e)
        {
            lbInfo.Visible = false;
        }

        private void tbNewPassword_TextChanged(object sender, EventArgs e)
        {
            lbInfo.Visible = false;
        }
    }
}
